package com.gooboot.kadmin.base.vo;

import com.gooboot.kadmin.base.constant.HttpConstant;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 封装统一的返回结果格式
 * @author 严富坤
 * @date 2020/8/29 11:36 下午
 * @version 1.0
 * @site http://www.gooboot.com
 */
@Data
@Accessors(chain = true)
public class HttpResult<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 结果数据
     */
    private T data;

    /**
     * 结果集
     */
    private List<T> listData;

    /**
     * 扩展字段
     */
    private Object extra;

    /**
     * 分页信息
     */
    private PageInfo pageInfo;

    private HttpResult(){}

    public static <T> HttpResult<T> ok(){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_SUCCESS;
        result.message = HttpConstant.MSG_SUCCESS;

        return result;
    }

    public static <T> HttpResult<T> ok(String msg){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_SUCCESS;
        result.message = msg;

        return result;
    }

    public static <T> HttpResult<T> ok(T model){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_SUCCESS;
        result.message = HttpConstant.MSG_SUCCESS;
        result.data = model;
        return result;
    }

    public static <T> HttpResult<T> ok(String msg,T model){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_SUCCESS;
        result.message = msg;
        result.data = model;

        return result;
    }

    public static <T> HttpResult<T> ok(List<T> list){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_SUCCESS;
        result.message = HttpConstant.MSG_SUCCESS;
        result.data = list == null?new ArrayList<T>():list;
        return result;
    }

    public static HttpResult error(){
        HttpResult result = new HttpResult();
        result.code = HttpConstant.CODE_ERROR;
        result.message = HttpConstant.MSG_ERROR;
        return result;
    }

    public static HttpResult error(String msg){
        HttpResult result = new HttpResult();
        result.message = msg;
        result.code = HttpConstant.CODE_ERROR;
        return result;
    }
}
